#!/usr/bin/python
# -*- coding: utf-8 -*-  
"""
@Project : hello 
@file : oracle_to_csv_icinl.py
@Author : sheen
@time : 2025/5/20 11:45
@func : 单个耗时SQl查询
"""
import logging
import time
from typing import List

from com.cn.for_cdc.common.OracleQueryExecutor import OracleQueryExecutor
from com.cn.for_cdc.common.cdc_conmons import oracle_adm_connections
from com.cn.for_cdc.common.log_helper import configure_logging


def write_large_csv(data: List[dict], filename: str) -> None:
    """
    将数据写入CSV文件，使用pandas优化写入速度
    """
    import pandas as pd
    df = pd.DataFrame(data)
    df.to_csv(filename, index=False, encoding='utf-8', quoting=1)

def main():
    """使用示例"""
    configure_logging()

    # 初始化执行器
    config = oracle_adm_connections['icibe_pro']
    executor = OracleQueryExecutor(config)

    # 复杂查询示例
    complex_sql = """
    
 SELECT /*+ parallel(16) */sc.ROW_ID AS contact_id, NVL(ATTRIB_11,'N')
FROM siebel.s_contact@SBL sc,siebel.S_CONTACT_X@SBL scx
WHERE sc.BU_ID = '1-7V7X' AND sc.row_id = scx.par_row_id 
AND sc.LAST_UPD < sysdate -1 / 12
MINUS 
SELECT /*+ parallel(16) */sbc.CONTACT_ID,NVL(PUSH_FLG,'N')
FROM STG_B_CONTACT sbc

    """

    # 执行单个查询
    try:
        start = time.time()
        result = executor.execute_query(complex_sql)
        print(result)
        # 将查询结果保存为csv
        write_large_csv(result, r'F:\workspace\pythonProject\hello\com\cn\for_cdc\output_data\20250522_icinl_17.csv')
        logging.info(f"获取到{len(result)}条差异数据")
        logging.info(f"总执行耗时: {time.time() - start:.2f}s")
    except Exception as e:
        pass


if __name__ == "__main__":
    main()
